import { _decorator, Button, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { EVENT_TYPE, MISSION_TYPE, REWARD_TYPE } from '../../Cube/Levels';
import DatasSpace from '../Tool/DatasSpace';
import SkdComm from '../SkdComm';
import EventMgr from '../Tool/EventMgr';
import { showTips } from '../Tips/Tips';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('DailyShareItem')
export class DailyShareItem extends Component {

  @property(Sprite)
  shareTips: Sprite = null;

  @property(SpriteFrame)
  tipsSF: SpriteFrame[] = [];

  @property(SpriteFrame)
  bgSF: SpriteFrame[] = [];

  @property(Node)
  rewItem: Node = null;

  @property(Label)
  coinNum: Label = null;

  @property(SpriteFrame)
  rewSF: SpriteFrame[] = [];

  private config = null
  private id: number = 0;
  start() {
    EventMgr.on(EVENT_TYPE.EVENT_UPD_SHARE, this.initBtn, this);
  }

  protected onDestroy(): void {
    EventMgr.off(EVENT_TYPE.EVENT_UPD_SHARE, this.initBtn, this);
  }

  update(deltaTime: number) {

  }

  init(id, config) {
    this.id = id;
    this.config = config
    this.shareTips.spriteFrame = this.tipsSF[id];
    this.node.getComponent(Sprite).spriteFrame = this.bgSF[config.type];

    if (!DatasSpace.Instance.isSameDay) DatasSpace.Instance.setDailyShare(this.id, false);
    this.initReward();
    this.initBtn();
  }

  initReward() {
    let rewInfo = this.config.rewardList[0]
    this.rewItem.getChildByName('Icon').getComponent(Sprite).spriteFrame = this.rewSF[rewInfo[0]];

    let coinInfo = this.config.rewardList[1];
    this.coinNum.string = `x${coinInfo[1]}`;
  }

  initBtn() {
    let btn_share = this.node.getChildByName('shareBtn');
    let btn_get = this.node.getChildByName('getBtn');

    const count = DatasSpace.Instance.getInt('DailyMission_' + MISSION_TYPE.SHARE, 0);

    if (count >= this.config.shareCount) {
      btn_share.active = false;
      btn_get.active = true;
      let isGet = DatasSpace.Instance.getDailyShare(this.id);
      btn_get.getComponent(Sprite).grayscale = isGet;
      btn_get.getComponent(Button).interactable = !isGet;
    } else {
      btn_share.active = true;
      btn_get.active = false;
    }
  }

  private btn_share() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    SkdComm.Instance.shareTo(
      () => {
        EventMgr.emit(EVENT_TYPE.EVENT_UPD_SHARE);
      }
    );
  }

  private btn_get() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    this.getReward();
    showTips('领取成功');

    for (let i = 0; i < this.config.rewardList.length; i++) {
      let rewConfig = this.config.rewardList[i];
      let rewType = rewConfig[0];
      let Rew_Num = rewConfig[1];
      switch (rewType) {
        case REWARD_TYPE.CHEHUI:
          DatasSpace.Instance.propNumArr[3] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.FANPAI:
          DatasSpace.Instance.propNumArr[1] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.XIAOCHU:
          DatasSpace.Instance.propNumArr[2] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.XIPAI:
          DatasSpace.Instance.propNumArr[0] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.COIN:
          DatasSpace.Instance.goldNum += Rew_Num;
          DatasSpace.Instance.setGoldNum();
          break;
        default:
          break;
      }
    }


  }

  getReward() {
    const count = DatasSpace.Instance.getInt('DailyMission_' + MISSION_TYPE.SHARE, 0);
    let isGet = DatasSpace.Instance.getDailyShare(this.id);
    if (count >= this.config.shareCount && !isGet) {
      DatasSpace.Instance.setDailyShare(this.id, true);
      EventMgr.emit(EVENT_TYPE.EVENT_UPD_SHARE);
    }
  }

}


